#ifndef _COLUMN_AND_TIME_H
#define _COLUMN_AND_TIME_H

#include "GraphicElement.h"

#include <string.h>
#include <mmsystem.h>
#include <tchar.h>

class ColumnsAndTime: public GraphicElement
{
private:
	int count;
	float *heightData;
	float *heightData2;
	int lastTime;
	int playingTime;
	int drawMode;
	int drawModeCount;
	PrivateFontCollection *pfc;
	Font *font;

public:
	ColumnsAndTime(int x, int y, int width, int height, int count, HWND hWnd): GraphicElement(x, y, width, height, hWnd)
	{
		this->count = count;
		heightData = new float[count];
		heightData2 = new float[count];
		for(int i = 0; i < count; i++)
		{ 
			heightData[i] = 0; 
			heightData2[i] = 0; 
		}
		playingTime = 0;

		drawMode = 5; // default
		drawModeCount = 11;




		pfc = new PrivateFontCollection();
		pfc->AddFontFile(L"Ventouse.ttf");


		// How many font families are in the private collection?
	   int countf = pfc->GetFamilyCount();

	   // Allocate a buffer to hold the array of FontFamily objects returned by
	   // the GetFamilies method.
	   FontFamily* pFontFamily = (FontFamily*)malloc(countf * sizeof(FontFamily));

	   // Get the array of FontFamily objects.
	   int found = 0;
	   pfc->GetFamilies(countf, pFontFamily, &found);

	   font = NULL;
	   if(found != 0)
	   {

			// Get the font family name.
			WCHAR                 familyName[50];
			pFontFamily[0].GetFamilyName(familyName);
 
			// Pass the family name and the address of the private collection to a
			// Font constructor.
			font = new Font(familyName, 35, FontStyleRegular,
								 UnitPixel, pfc);
	   }



	}

	~ColumnsAndTime()
	{
		delete[] heightData;
		delete[] heightData2;
		delete pfc;
		delete font;
	}


	void Draw(HDC hdc)
	{
		if(redraw)
		{
			Bitmap bmp(dimensions->width,dimensions->height);
			Graphics *fromImg = Graphics::FromImage(&bmp);


			//SolidBrush  brush(Color(255, 0, 0, 255));
			//SolidBrush  brush2(Color(255, 255, 200, 000));
	

			SolidBrush  brushStulpeliai(COLUMNS_AND_TIME_COLUMNS_COLOR);
			SolidBrush  brushBackground(COLUMNS_AND_TIME_BACKGROUND_COLOR);
			Pen			penBorder(COLUMNS_AND_TIME_BORDER_COLOR);
			Pen			penStulpeliai(COLUMNS_AND_TIME_COLUMNS_COLOR);


			


			
			fromImg->FillRectangle(&brushBackground, 0, 0, dimensions->width, dimensions->height);
			//fromImg->DrawRectangle(&penBorder, 0, 0, dimensions->width-1, dimensions->height-1);
			//graphics.DrawRectangle(&pen, 5, 113, 250, 8);
	
			//int drawmode = 2;
			//drawMode = 1;

			float heightMultiplier = (float)(dimensions->height-1) / 100;

			int averageColumnSize = (int)((heightData[0]*100)*heightMultiplier);

			for(int i = 0; i < count-1; i++)
			{// max height 102
				//SolidBrush  zalia(Color(255, 255*heightData[i], 180, 255-255*heightData[i]));
				switch(this->drawMode)
				{
				case 0:
					{
						fromImg->FillRectangle(&brushStulpeliai, 2 + (i*3), dimensions->height-(int)((heightData[i]*100)*heightMultiplier) -1  , 2, (int)((heightData[i]*100)*heightMultiplier));
						break;
					}

				case 1:
					{
						//if(i%2 == 0)
						fromImg->FillRectangle(&brushStulpeliai, 2 + (i*3), dimensions->height-(int)((heightData[i]*100)*heightMultiplier) -1  , 2, (int)((heightData[i]*100)*heightMultiplier));
						//else
						fromImg->FillRectangle(&brushStulpeliai, 2 + (i*3), 1  , 2, (int)((heightData[i]*100)*heightMultiplier));
						break;
					}

				case 2:
					{

						fromImg->FillRectangle(&brushStulpeliai, 2 + (i*3), dimensions->height/2-(int)((heightData[i]*50)*heightMultiplier)  , 2, (int)((heightData[i]*50)*heightMultiplier));
						fromImg->FillRectangle(&brushStulpeliai, 2 + (i*3), dimensions->height/2  , 2, (int)((heightData[i]*50)*heightMultiplier));
						break;
					}

				case 3:
					{
						fromImg->FillRectangle(&brushStulpeliai, 2 + (i*3), dimensions->height-(int)((heightData[i]*50)*heightMultiplier) -1  , 2, (int)((heightData[i]*50)*heightMultiplier));
						fromImg->FillRectangle(&brushStulpeliai, 2 + (i*3), 1  , 2, (int)((heightData[i]*50)*heightMultiplier));
						/*
						if(i%2 == 0)
						fromImg->FillRectangle(&zalia, 2 + (i*4), height-(int)(heightData[i]*50) -1  , 3, (int)(heightData[i]*100));
						else
						fromImg->FillRectangle(&zalia, 2 + (i*4), 1  , 3, (int)(heightData[i]*50));*/

						break;
					}

				case 4:
					{

						fromImg->FillRectangle(&brushStulpeliai, 2 + (i*3), dimensions->height/2-(int)((heightData[i]*50)*heightMultiplier)  , 2, (int)((heightData[i]*50)*heightMultiplier));
						fromImg->FillRectangle(&brushStulpeliai, 2 + (i*3), dimensions->height/2  , 2, (int)((heightData[i]*50)*heightMultiplier));
						break;
					}

				case 5:
					{
						break;
					}

				case 6:
					{

						fromImg->FillRectangle(&brushStulpeliai, 2 + (i*3), dimensions->height-averageColumnSize -1  , 2, averageColumnSize);
						averageColumnSize = (averageColumnSize + (int)((heightData[i]*100)*heightMultiplier))/2;
						break;
					}

				case 7:
					{

						fromImg->FillRectangle(&brushStulpeliai, 2 + (i*3), dimensions->height-(int)((heightData[i]*100)*heightMultiplier) -6  , 2, 5);
						break;
					}

				case 8:
					{
						if(i+1 < count-1)
						{
							fromImg->DrawLine(&penStulpeliai, 2 + (i*3), 
								dimensions->height-(int)((heightData[i]*100)*heightMultiplier) -1,
								2 + ((i+1)*3),
								dimensions->height-(int)((heightData[i+1]*100)*heightMultiplier) -1);
						}
						
						break;
					}


				case 9:
					{
						fromImg->FillRectangle(&brushStulpeliai, 2 + (i*3), dimensions->height-(int)((heightData[i]*100)*heightMultiplier) -1  , 2, (int)((heightData[i]*100)*heightMultiplier));
						fromImg->FillRectangle(&brushStulpeliai, 2 + (i*3), dimensions->height-(int)((heightData2[i]*100)*heightMultiplier) -3  , 2, 3);
						break;
					}



				case 10:
					{
						if(i+1 < count-1)
						{
							fromImg->DrawLine(&penStulpeliai, 2 + (i*3), 
								dimensions->height-(int)((heightData[i]*100)*heightMultiplier) -1,
								2 + ((i+1)*3),
								dimensions->height-(int)((heightData[i+1]*100)*heightMultiplier) -1);


							fromImg->DrawLine(&penStulpeliai, 2 + (i*3), 
								dimensions->height-(int)((heightData2[i]*100)*heightMultiplier) -1,
								2 + ((i+1)*3),
								dimensions->height-(int)((heightData2[i+1]*100)*heightMultiplier) -1);

						}
						
						break;
					}


					break;
				}
		
			}
   
			//AddFontResourceEx(

			WCHAR *txt = new WCHAR[10];
			_stprintf(txt, _T("%02d:%02d:%02d"), playingTime / 1000 / 60 / 60, playingTime / 1000 / 60 % 60, playingTime / 1000 % 60);
			fromImg->DrawString(txt, -1, font, PointF(15, 15), &SolidBrush(COLUMNS_AND_TIME_TEXT_COLOR));
			//fromImg->DrawString(txt, -1, &Font(&FontFamily(L"Arial"), 40, FontStyleRegular, UnitPixel), PointF(15, 15), &SolidBrush(COLUMNS_AND_TIME_TEXT_COLOR));
			delete[] txt;

			//fromImg->FillRectangle(&zalia, 5, 113, 250, 8);
	
			Graphics    graphics(hdc);
			graphics.DrawImage(&bmp, dimensions->x, dimensions->y, dimensions->width, dimensions->height);

			delete fromImg;
			redraw = false;
		}
	}

	void UpdateColumnData(float *fl, int time)
	{
		int lastT = playingTime;
		playingTime = time;
		int t = timeGetTime();
		//float *sum = new float[count];
	
		if(drawMode != 4)
		{
			int index = 0;
			for(int i = 0; i < count; i++)
			{
				float suma = 0;
				for(int j = 0; j < 1; j++)
				{
					suma += fl[index];
					index++;
				}
				//suma = 0.1;
		
				float sk = (float)suma*(i+1)*0.8;//  /player->getVolume()*5;
				if(sk > 1.0) sk = 1.0;


				heightData[i] = heightData[i] - (0.0015 * (t-lastTime));
				heightData2[i] = heightData2[i] - (0.0003 * (t-lastTime));

				if(heightData[i] < 0) heightData[i] = 0.0;
				if(heightData[i] > 1) heightData[i] = 1.0;

				if(heightData2[i] < 0) heightData2[i] = 0.0;
				if(heightData2[i] > 1) heightData2[i] = 1.0;
		

				if(sk > heightData[i])
				{
					heightData[i] = sk;
					if(heightData[i] > heightData2[i]) heightData2[i] = heightData[i];
				}



				//heightData[i] = 1;
		
			}
		}
		else
		{
			float sm = 0;
			for(int i = 1; i < 64; i ++)
			{
				heightData[i-1] = heightData[i];
				heightData2[i-1] = heightData2[i];
				sm += fl[i-1];
			}
		
			sm = sm / 2;
			if(sm > 1.0) sm = 1.0;

			heightData[63] = sm;
			heightData2[63] = sm;
		}


		if(drawMode != 5 || (lastT / 1000 % 60) != (time / 1000 % 60))
		{
			UpdateGraphics();
		}

		


		
		lastTime = t;
	}



	void MouseDown()
	{
		LPPOINT p = new POINT;
		GetCursorPos(p);
		ScreenToClient(hWnd, p);

		//drawMode++;
			//if(drawMode >= drawModeCount) drawMode = 0;
		if(p->x >= dimensions->x+1 && p->x <= dimensions->x + dimensions->width-1 &&
			p->y >= dimensions->y && p->y <= dimensions->y + dimensions->height)
		{
			drawMode++;
			if(drawMode >= drawModeCount) drawMode = 0;

		}

		delete[] p;
		//return drawMode;
	}
	int setDrawMode(int m)
	{
		if(m < 0) m = 0;
		if(m > drawModeCount) m = drawModeCount;
		drawMode = m;
		return drawMode;
	}

	int getDrawMode()
	{
		return drawMode;
	}


};


#endif